﻿using HCMS.CommonDtos;
using HCMS.Entities.Base;
using HCMS.IRepositories.Base;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.EntityFrameworkCore;

namespace HCMS.Repositories.Base
{
    /// <summary>
    /// 敏感词 仓储层
    /// </summary>
    public class SensitiveWordsRepository : BaseRepository<SensitiveWords>, ISensitiveWordsRepository
    {
        public SensitiveWordsRepository(IDbContextProvider<BaseDbContext> dbContextProvider)
            : base(dbContextProvider)
        {
        }

        /// <summary>
        /// 管理 列表
        /// </summary>
        /// <param name="searchArgs">搜索 参数</param>
        /// <param name="pager">分页 参数</param>
        /// <returns></returns>
        public async Task<ListPagerDto<SensitiveWords>> ManageList(SearchArgsDto searchArgs, PagerArgsDto pager)
        {
            var query = await GetQueryableAsync();

            if (!string.IsNullOrWhiteSpace(searchArgs.SearchName) && !string.IsNullOrWhiteSpace(searchArgs.SearchKey))
            {
                switch (searchArgs.SearchName)
                {
                    case "title":
                        query = query.Where(t => t.Title.Contains(searchArgs.SearchKey));
                        break;
                }
            }

            if (searchArgs.Status != 9999)
            {
                query = query.Where(t => t.Status == searchArgs.Status);
            }

            if (searchArgs.ClassId > 0)
            {
                query = query.Where(t => t.ClassId == searchArgs.ClassId);
            }

            var count = await query.CountAsync();
            pager.RowCount = count;

            var list = new List<SensitiveWords>();

            if (count > 0)
            {
                query = query.OrderByDescending(t => t.Id);

                list = await query
                           .Skip((pager.CurrentPageIndex - 1) * pager.PageSize)
                           .Take(pager.PageSize)
                           .ToListAsync();
            }

            var result = new ListPagerDto<SensitiveWords>()
            {
                Data = list ?? new List<SensitiveWords>(),
                PagerInfo = pager
            };

            return result;
        }

        /// <summary>
        /// 全部 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<SensitiveWords>> AllList()
        {
            var query = await GetQueryableAsync();
            var result = await query.ToListAsync();
            return result ?? new List<SensitiveWords>();
        }
    }
}
